package com.tanhua.server.service;

import cn.hutool.core.collection.CollUtil;
import com.tanhua.commons.utils.SoulTestResult;
import com.tanhua.dubbo.api.*;
import com.tanhua.model.domain.AAA;
import com.tanhua.model.domain.Option;
import com.tanhua.model.domain.QuestionnaireReport;
import com.tanhua.model.domain.QuestionnaireResult;
import com.tanhua.model.vo.QuestionnaireVo;
import com.tanhua.model.vo.TestResultVo;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author fff
 * @date 2021/12/11 12:35
 **/
@Service
public class TestSoulService {

    @DubboReference
    private QuestionnaireApi questionnaireApi;
    @DubboReference
    private SoulQuestionApi soulQuestionApi;
    @DubboReference
    private OptionApi optionApi;
    @DubboReference
    private QuestionnaireResultApi questionnaireResultApi;
    @DubboReference
    private QuestionnaireReportApi questionnaireReportApi;


    //问卷列表
    public List questionnaireList() {
      List<QuestionnaireVo> vos = questionnaireApi.find();
      return vos;
    }
    //提交问卷
    public String commit(Map map) {
        Long id = UserHolder.getUserId();
        int sum = 0;
        String level =null;

        List<AAA> list = (ArrayList<AAA>)map.get("answers");
        //试题编号
        List<Option> optionList = optionApi.selectlist(list);
        List<Integer> score1 = CollUtil.getFieldValues(optionList, "score", Integer.class);
        for (Integer integer : score1) {
            sum = sum + integer;
        }


        QuestionnaireResult questionnaireResult = null;
        if(sum>=0 && sum <=20){
            questionnaireResult = questionnaireResultApi.findByScore("1");
        }else if(sum>=21 && sum <=40){
            questionnaireResult = questionnaireResultApi.findByScore("2");
        }else if(sum>=41 && sum <=55){
           questionnaireResult = questionnaireResultApi.findByScore("3");
        }else {
            questionnaireResult = questionnaireResultApi.findByScore("4");
        }
        //questionnaireResult,level,id
        QuestionnaireReport report = new QuestionnaireReport();

        report.setUserId(id);
        report.setContent(questionnaireResult.getContent());
        report.setCover(SoulTestResult.C);
        /*
            private Integer extroversion;
            private Integer judgement;
            private Integer abstraction;
            private Integer retionality;
         */
        report.setExtroversion("50%");
        report.setJudgement("50%");
        report.setAbstraction("50%");
        report.setRetionality("50%");


        questionnaireReportApi.save(report);

        QuestionnaireReport questionnaireReport=questionnaireReportApi.findOne(level,id);


        return questionnaireReport.getId().toString();
    }


    public TestResultVo lookResult(String id) {

      QuestionnaireReport report =   questionnaireReportApi.findById(id);

        TestResultVo testResultVo = new TestResultVo();

        testResultVo.setConclusion(report.getContent());
        testResultVo.setCover(report.getCover());

        String abstraction = report.getAbstraction();
        String extroversion = report.getExtroversion();
        String judgement = report.getJudgement();
        String retionality = report.getRetionality();
        ArrayList<Object> list = new ArrayList<>();

        list.add(abstraction);
        list.add(extroversion);
        list.add(retionality);
        list.add(judgement);

        testResultVo.setDimensions(list);


        return testResultVo;
    }
}
